package ca.etsmtl.log720.lab2.servlets;

import java.io.IOException;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import ca.etsmtl.log720.lab2.beans.Dossier;
import ca.etsmtl.log720.lab2.beans.Infraction;
import ca.etsmtl.log720.lab2.beans.NoPermisInvalideException;
import ca.etsmtl.log720.lab2.dao.DossierDAO;
import ca.etsmtl.log720.lab2.dao.InfractionDAO;

/**
 * Servlet implementation class MyServlet
 */
public class MyServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public MyServlet() {
		super();	 
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

			String action = request.getParameter("action");
			String address = "index.jsp";
			
			//Toute requête GET est identifiée d'une action
			//L'action concerne l'information à afficher
		    if (action == null) {
		    	address = "/WEB-INF/error.jsp";
		    } 
		    //Requête = Afficher les dossiers du système
		    else if (action.equals("dossiers")) {
		    	address = "/Dossiers.jsp";
		    	DossierDAO dao = DossierDAO.getInstance();
		    	request.setAttribute("dossiers", dao.selectDossiers());
		    } 
		    //Requête = Afficher les infractions du système
		    else if (action.equals("infractions")) {
		    	address = "/Infractions.jsp";
		    	InfractionDAO dao = InfractionDAO.getInstance();
		    	request.setAttribute("infractions", dao.selectInfractions());
		    } 
		    //Requête = Consulter un dossier
		    else if (action.equals("dossier")) {
		      address = "/Dossier.jsp";
		      int idDossier = Integer.parseInt(request.getParameter("idDossier"));
		      Dossier d = DossierDAO.getInstance().selectDossier(idDossier);
		      if(d!=null){
		    	  InfractionDAO infdao = InfractionDAO.getInstance();
			      request.setAttribute("dossier", d);
			      request.setAttribute("infractions", infdao.selectInfractions(idDossier));
			      request.setAttribute("sysVide", infdao.selectInfractions().size()==0);//Disable link 'ajouterInfraction'
		      }
		      else{
		    	  address = "/ErreurRequete.jsp";
		      }
		    } 
		    //Requête = Ajouter un dossier
		    else if(action.equals("ajouterDossier")){
		    	address = "/AjouterDossier.jsp";
		    }
		    //Requête = Ajouter une infraction
		    else if(action.equals("ajouterInfraction")){
		    	address = "/AjouterInfraction.jsp";
		    }
		    //Requête = Ajouter une infraction au dossier en consultation
		    else if(action.equals("ajouterInfractionAuDossier")){
		    	int idDossier = Integer.parseInt(request.getParameter("idDossier"));
		    	Dossier d = DossierDAO.getInstance().selectDossier(idDossier);
		    	request.setAttribute("dossier", d);
		    	if(d!=null){
			    	request.setAttribute("infractions", InfractionDAO.getInstance().selectInfractions());
			    	address = "/AjouterInfractionAuDossier.jsp";
		    	}
		    	else{
		    		address = "/ErreurRequete.jsp";
		    	}
		    } 
		    //Revenir à la page index
		    else if(action.equals("index")){
		    	address = "/index.jsp";
		    } 
		    //Requête = se déconnecter
		    else if(action.equals("logoff")){
		    	HttpSession session = request.getSession(true);
		    	session.invalidate();
		    	response.sendRedirect(request.getContextPath()+"/index.jsp");
		    	return;   	
		    } 
		    
		    else {
		    	address = "/404.jsp";
		      
		    }
		    RequestDispatcher dispatcher =
		      request.getRequestDispatcher(address);
		    dispatcher.forward(request, response);
		
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String action = request.getParameter("action");
		String address = "index.jsp";
		
		//Toute requête POST est identifiée d'une action
		//L'action concerne la modification aux données
	    if (action == null) {
	      address = "/WEB-INF/error.jsp";
	    //Action = Ajouter le dossier saisi
	    } else if(action.equals("ajouterDossier")){
	    	String message = "Enregistrement reussi! Dossier ajouté.";
	    	
	    	address = "/AjouterDossier.jsp";
	    	String nom = request.getParameter("nom");
	    	String prenom = request.getParameter("prenom");
	    	String noPermis = request.getParameter("noPermis");
	    	String noPlaque = request.getParameter("noPlaque");
	    	if(nom!=null && prenom!=null && noPermis!=null && noPlaque!=null){
	    		Dossier d = new Dossier(-1, nom,prenom,noPermis,noPlaque);
	    		try{
		    		boolean insertReussis = DossierDAO.getInstance().insertDossier(d);
		    		if(!insertReussis)
		    			message = "L'enregistrement à échoué!";
	    		}
	    		catch(NoPermisInvalideException npie){
	    			message = npie.getMessage();
	    		}
	    	}
	    	request.setAttribute("Message", message);
    		
	    //Action = Ajouter l'infraction saisi	
	    } else if(action.equals("ajouterInfraction")){

	    	String message = "Enregistrement reussi! Infraction ajoutée.";
	    	int niveau;
	    	try{
	    		niveau = Integer.parseInt(request.getParameter("niveau"));
	    	}
	    	catch(NumberFormatException e){
	    		niveau = -1;
	    	}
	    	
	    	if(niveau> Infraction.MAX_NIVEAU || niveau < Infraction.MIN_NIVEAU){
	    		message = "Enregistrement annulé: Le niveau de l'infraction doit se situer entre "+ 
	    					Infraction.MIN_NIVEAU + " et " + Infraction.MAX_NIVEAU + "!";
	    	}
	    	else{
		    	String description = request.getParameter("description");
		    	if(description.length() > 0){
			    	Infraction infraction = new Infraction();
			    	infraction.setNiveau(niveau);
			    	infraction.setDescription(description);
		    		boolean insertReussis = InfractionDAO.getInstance().insertInfraction(infraction);
		    		if(!insertReussis)
		    			message = "L'enregistrement à échoué!";
		    	}
		    	else{
		    		message="Enregistrement annulé: Spécifiez une description!";
		    	}
	    		
	    	}
	    	address = "/AjouterInfraction.jsp";
	    	request.setAttribute("Message", message);
	    	
	    	//Action = Ajouter l'infraction saisi au dossier en consultation		
	    	} else if(action.equals("ajouterInfractionAuDossier")){

	    	int infractionID = Integer.parseInt(request.getParameter("infractionID"));
	    	int dossierID = Integer.parseInt(request.getParameter("dossierID"));
    		boolean insertReussis = DossierDAO.getInstance().insertInfractionAuDossier(dossierID, infractionID);
	    
	    	address = "/Dossier.jsp";
		    DossierDAO daoDossier = DossierDAO.getInstance();
		    InfractionDAO daoInfraction = InfractionDAO.getInstance();
		    request.setAttribute("dossier", daoDossier.selectDossier(dossierID));
		    request.setAttribute("infractions", daoInfraction.selectInfractions(dossierID));
	    	request.setAttribute("sysVide", false);	//Enable link 'ajouterInfraction'
	    }
	    
	    RequestDispatcher dispatcher =
	      request.getRequestDispatcher(address);
	    dispatcher.forward(request, response);
	
	}

}
